/*
 * LEDs driver for GPADCs
 *
 * Copyright (C) 2007 8D Technologies inc.
 * Raphael Assenat <raph@8d.com>
 * Copyright (C) 2008 Freescale Semiconductor, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */
#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/gpio/consumer.h>
#include <linux/kernel.h>
#include <linux/leds.h>
#include <linux/leds_gpadc.h>
#include <linux/iio/iio.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/slab.h>

struct gpadc_led_data {
	struct led_classdev cdev;
	unsigned	channel_number;
};

static inline struct gpadc_led_data *
			cdev_to_gpadc_led_data(struct led_classdev *led_cdev)
{
	return container_of(led_cdev, struct gpadc_led_data, cdev);
}

static void gpadc_led_set(struct led_classdev *led_cdev,
	enum led_brightness value)
{
	struct gpadc_led_data *led_dat = cdev_to_gpadc_led_data(led_cdev);
	int level;

	if (value == LED_OFF)
		level = 0;
	else
		level = 1;

	lombo_write_ainx_output(led_dat->channel_number, level);
}

static enum led_brightness gpadc_led_get(struct led_classdev *led_cdev)
{
	struct gpadc_led_data *led_dat = cdev_to_gpadc_led_data(led_cdev);

	return lombo_read_ainx_output(led_dat->channel_number);
}

static int create_gpadc_led(const struct gpadc_led *template,
	struct gpadc_led_data *led_dat, struct device *parent, struct device_node *np)
{
	int state;

	led_dat->channel_number = template->channel_number;
	led_dat->cdev.name = template->name;
	led_dat->cdev.default_trigger = template->default_trigger;
	led_dat->cdev.default_trigger = template->default_trigger;
	led_dat->cdev.brightness = LED_OFF;
	led_dat->cdev.flags = GPIOF_OUT_INIT_LOW;
	led_dat->cdev.brightness_set = gpadc_led_set;
	led_dat->cdev.brightness_get = gpadc_led_get;

	state = (template->default_state == LEDS_GPADC_DEFSTATE_ON);
	led_dat->cdev.brightness = state;

	lombo_write_ainx_output(led_dat->channel_number, state);

	return led_classdev_register(parent, &led_dat->cdev);
}

struct gpadc_leds_priv {
	int num_leds;
	struct gpadc_led_data leds[];
};

static inline int sizeof_gpadc_leds_priv(int num_leds)
{
	return sizeof(struct gpadc_leds_priv) +
		(sizeof(struct gpadc_led_data) * num_leds);
}

static struct gpadc_leds_priv *gpadc_leds_create(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct fwnode_handle *child;
	struct gpadc_leds_priv *priv;
	int count, ret;

	count = device_get_child_node_count(dev);
	if (!count)
		return ERR_PTR(-ENODEV);

	priv = devm_kzalloc(dev, sizeof_gpadc_leds_priv(count), GFP_KERNEL);
	if (!priv)
		return ERR_PTR(-ENOMEM);

	device_for_each_child_node(dev, child) {
		struct gpadc_led_data *led_dat = &priv->leds[priv->num_leds];
		struct gpadc_led led = {};
		const char *state = NULL;
		struct device_node *np = to_of_node(child);

		ret = fwnode_property_read_string(child, "label", &led.name);
		if (ret && IS_ENABLED(CONFIG_OF) && np)
			led.name = np->name;
		if (!led.name) {
			fwnode_handle_put(child);
			return ERR_PTR(-EINVAL);
		}

		ret = of_property_read_u32(np, "gpadc_channel", &(led.channel_number));
		if(ret < 0) {
				dev_err(&pdev->dev, "GPADC:channelnumber %d (%s) get failed!\n",
				led_dat->channel_number, led.name);
		}

		fwnode_property_read_string(child, "linux,default-trigger",
					    &led.default_trigger);

		if (!fwnode_property_read_string(child, "default-state",
						 &state)) {
			if (!strcmp(state, "on"))
				led.default_state = LEDS_GPADC_DEFSTATE_ON;
			else
				led.default_state = LEDS_GPADC_DEFSTATE_OFF;
		}

		ret = create_gpadc_led(&led, led_dat, dev, np);
		if (ret < 0) {
			fwnode_handle_put(child);
			return ERR_PTR(ret);
		}
		led_dat->cdev.dev->of_node = np;
		priv->num_leds++;
	}

	return priv;
}

static const struct of_device_id of_gpadc_leds_match[] = {
	{ .compatible = "gpadc-leds", },
	{},
};

MODULE_DEVICE_TABLE(of, of_gpadc_leds_match);

static int gpadc_led_probe(struct platform_device *pdev)
{
	struct gpadc_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
	struct gpadc_leds_priv *priv;
	int i, ret = 0;

	if (pdata && pdata->num_leds) {
		priv = devm_kzalloc(&pdev->dev,
				sizeof_gpadc_leds_priv(pdata->num_leds),
					GFP_KERNEL);
		if (!priv)
			return -ENOMEM;

		priv->num_leds = pdata->num_leds;
		for (i = 0; i < priv->num_leds; i++) {
			ret = create_gpadc_led(&pdata->leds[i], &priv->leds[i],
					      &pdev->dev, NULL);
			if (ret < 0)
				return ret;
		}
	} else {
		priv = gpadc_leds_create(pdev);
		if (IS_ERR(priv))
			return PTR_ERR(priv);
	}

	platform_set_drvdata(pdev, priv);

	return 0;
}

static void gpadc_led_shutdown(struct platform_device *pdev)
{
	struct gpadc_leds_priv *priv = platform_get_drvdata(pdev);
	int i;

	for (i = 0; i < priv->num_leds; i++) {
		struct gpadc_led_data *led = &priv->leds[i];

		if (!(led->cdev.flags & LED_RETAIN_AT_SHUTDOWN))
			gpadc_led_set(&led->cdev, LED_OFF);
	}
}

static struct platform_driver gpadc_led_driver = {
	.probe		= gpadc_led_probe,
	.shutdown	= gpadc_led_shutdown,
	.driver		= {
		.name	= "leds-gpadc",
		.of_match_table = of_gpadc_leds_match,
	},
};

module_platform_driver(gpadc_led_driver);

MODULE_AUTHOR("Raphael Assenat <raph@8d.com>, Trent Piepho <tpiepho@freescale.com>");
MODULE_DESCRIPTION("GPADC LED driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:leds-gpadc");
